package service

import (
	"context"
	"errors"
	"fmt"
	"ldaccount/internal/dao"
	"ldaccount/pkg/util"

	"ldaccount/internal/model"
	"ldaccount/internal/object"
	"ldaccount/pkg/lueerror"
)

type AccountsSrv struct {
	*Service
	Name string
}

var accSrv *AccountsSrv

// InitAccountsSrv initialize
func InitAccountsSrv() {
	accSrv = &AccountsSrv{
		Service: service,
		Name:    "account_service",
	}
}

// GetAccountsSrv get implementation
func GetAccountsSrv() *AccountsSrv {
	return accSrv
}

// ImportAccounts 账户导入
func (srv *AccountsSrv) ImportAccounts(ctx context.Context, accs []*object.AccImport, origin string) (int64, lueerror.LueError) {
	if len(accs) < 1 {
		return 0, nil
	}
	num := len(accs)
	accsM := make([]*model.Accounts, len(accs))
	now := util.GetNow()
	for idx, acc := range accs {
		t := &model.Accounts{
			GID:         acc.GID,
			Mobile:      acc.Mobile,
			Email:       acc.Email,
			Status:      model.AccountsOk,
			Password:    acc.Password,
			UserName:    acc.UserName,
			CountryCode: acc.CountryCode,
			Origin:      origin,
			CreatedAt:   util.TimestampToTime(acc.Created),
			UpdatedAt:   now,
		}
		accsM[idx] = t
	}

	ret, err := dao.Transaction(ctx, func(ctx context.Context) (interface{}, error) {
		var insertNum int64
		var start int
		step := 1000
		for start < num {
			end := start + step
			if end > num {
				end = num
			}
			t, err := dao.GetAccountsRepo().CreateAccounts(ctx, accsM[start:end])
			if err != nil {
				return 0, lueerror.PgErr(err)
			}
			insertNum += t
			start = end
		}
		return insertNum, nil
	})
	if err != nil {
		return 0, parseLueerror(err)
	}
	return ret.(int64), nil
}

func (srv *AccountsSrv) fieldsFormat(acc *model.Accounts) *object.AccInfo {
	if acc == nil {
		return nil
	}
	return &object.AccInfo{
		GID:      acc.GID,
		Mobile:   acc.Mobile,
		Email:    acc.Email,
		UserName: acc.UserName,
		Avatar:   acc.Avatar,
		Status:   acc.GetStatus(),
		Modified: util.TimeToTimestamp(acc.UpdatedAt),
	}
}

func (srv *AccountsSrv) verifyCode(ctx context.Context, mobile, _, code, codeType, codeOrigin string) lueerror.LueError {
	if srv.cfg.Base.Debug && code == "6666" {
		return nil
	}
	if codeOrigin != "mobile" {
		return lueerror.CodeVerifyFailed(errors.New("code 来源不支持"))
	}
	isOk, err := dao.GetSmsRepo().VerifyMobileCode(ctx, mobile, code, codeType)
	if err != nil {
		return lueerror.GrpcErr(err)
	}
	if !isOk {
		return lueerror.CodeVerifyFailed(errors.New("手机验证码错误"))
	}
	return nil
}

func (srv *AccountsSrv) registerCheck(ctx context.Context, mobile, email string) (*model.Accounts, lueerror.LueError) {
	if mobile == "" && email == "" {
		return nil, nil
	}
	var err error
	var accMail, accMobile *model.Accounts
	if mobile != "" {
		accMobile, err = dao.GetAccountsRepo().GetAccountByMobile(ctx, mobile)
		if err != nil {
			return nil, lueerror.PgErr(err)
		}
	}
	if email != "" {
		accMail, err = dao.GetAccountsRepo().GetAccountByEmail(ctx, email)
		if err != nil {
			return nil, lueerror.PgErr(err)
		}
	}
	// 邮箱手机都已经使用
	if accMail != nil && accMobile != nil {
		if accMail.ID != accMobile.ID {
			return nil, lueerror.MobileEmailHasUsed()
		}
		return accMobile, nil
	}
	// 只有邮箱注册了
	if accMail != nil {
		return accMail, nil
	}
	// 只有手机号注册了
	if accMobile != nil {
		return accMobile, nil
	}
	// 都未使用
	return nil, nil
}

// 激活账号
func (arv *AccountsSrv) activateAccount(ctx context.Context, acc *model.Accounts) (*model.Accounts, lueerror.LueError) {
	// 账号未激活
	if acc.IsActivate() {
		return acc, nil
	}
	acc.Activate()
	err := dao.GetAccountsRepo().UpdateAccountStatus(ctx, acc)
	if err != nil {
		return nil, lueerror.PgErr(err)
	}
	return acc, nil
}

// RegisterAccount 注册账号
func (srv *AccountsSrv) RegisterAccount(ctx context.Context, mobile, email, userName, password, code, codeType,
	countryCode, codeOrigin, origin string, isMD5 bool) (*object.AccInfo, lueerror.LueError) {
	// 检查手机号
	acc, errL := srv.registerCheck(ctx, mobile, email)
	if errL != nil {
		return nil, errL
	}
	if acc != nil {
		// 激活账号
		acc, errL = srv.activateAccount(ctx, acc)
		if errL != nil {
			return nil, errL
		}
		return srv.fieldsFormat(acc), nil
	}
	// 校验验证码
	if code != "" {
		if errL := srv.verifyCode(ctx, mobile, email, code, codeType, codeOrigin); errL != nil {
			return nil, errL
		}
	}
	var err error
	acc = model.NewAccounts(mobile, email, userName, "", countryCode, origin)
	_ = acc.SetStatus(model.AccountsOk)
	acc.SetPassword(password, isMD5)
	acc.ID, err = dao.GetAccountsRepo().CreateAccount(ctx, acc)
	if err != nil {
		return nil, lueerror.PgErr(err)
	}
	return srv.fieldsFormat(acc), nil
}

// AccountLogin 账号登陆
func (srv *AccountsSrv) AccountLogin(ctx context.Context, mobile, email, password, code, codeType, codeOrigin string, isMD5 bool) (*object.AccInfo, lueerror.LueError) {
	var acc *model.Accounts
	var err error
	if mobile != "" {
		acc, err = dao.GetAccountsRepo().GetAccountByMobile(ctx, mobile)
	} else if email != "" {
		acc, err = dao.GetAccountsRepo().GetAccountByEmail(ctx, email)
	} else {
		return nil, nil
	}
	if err != nil {
		return nil, lueerror.PgErr(err)
	}
	if acc == nil {
		return nil, lueerror.AccNotExit(errors.New("账号不存在"))
	}
	err = acc.AllowLogin()
	if err != nil {
		return nil, lueerror.PreventLogin(err)
	}
	// 认证信息校验
	if code != "" {
		// 校验验证码
		if !srv.cfg.Base.Debug || code != "6666" {
			if errL := srv.verifyCode(ctx, mobile, email, code, codeType, codeOrigin); errL != nil {
				return nil, errL
			}
		}
	} else {
		if !acc.PasswordVerify(password, isMD5) {
			return nil, lueerror.InvalidPassword(errors.New("密码错误"))
		}
	}

	ret := srv.fieldsFormat(acc)
	return ret, nil
}

// GetAccByMobileEmail 根据邮箱或手机号获取账号信息
func (srv *AccountsSrv) GetAccByMobileEmail(ctx context.Context, mobile, email string) (*object.AccInfo, lueerror.LueError) {
	var acc *model.Accounts
	var err error
	if mobile != "" {
		acc, err = dao.GetAccountsRepo().GetAccountByMobile(ctx, mobile)
	} else if email != "" {
		acc, err = dao.GetAccountsRepo().GetAccountByEmail(ctx, email)
	} else {
		return nil, nil
	}
	if err != nil {
		return nil, lueerror.PgErr(err)
	}
	if acc == nil {
		return nil, lueerror.AccNotExit(errors.New("账号不存在"))
	}
	ret := srv.fieldsFormat(acc)
	return ret, nil
}

// GetAccByGID 根据gid 获取用户信息
func (srv *AccountsSrv) GetAccByGID(ctx context.Context, gID string) (*object.AccInfo, lueerror.LueError) {
	acc, err := dao.GetAccountsRepo().GetAccountByGID(ctx, gID)
	if err != nil {
		return nil, lueerror.PgErr(err)
	}
	if acc == nil {
		return nil, lueerror.AccNotExit(errors.New("账号不存在"))
	}
	return srv.fieldsFormat(acc), nil
}

// GetAccountByLoginChannel 根据 email, origin获取账号信息
func (srv *AccountsSrv) GetAccountByLoginChannel(ctx context.Context, email, origin string) (*object.AccInfo, lueerror.LueError) {
	acc, err := dao.GetAccountsRepo().GetAccountByChannel(ctx, email, origin)
	if err != nil {
		return nil, lueerror.PgErr(err)
	}
	if acc == nil {
		return nil, lueerror.AccNotExit(errors.New("账号不存在"))
	}
	return srv.fieldsFormat(acc), nil
}

// UpdateAcc 更新账号信息
func (srv *AccountsSrv) UpdateAcc(ctx context.Context, gID, mobile, email, userName, avatar, countryCode, code, codeType, codeOrigin string) (*object.AccInfo, lueerror.LueError) {
	// 检查手机号
	if mobile != "" {
		acc, err := dao.GetAccountsRepo().GetAccountByMobile(ctx, mobile)
		if err != nil {
			return nil, lueerror.PgErr(err)
		}
		if acc != nil && acc.GID != gID {
			return nil, lueerror.MobileHasUsed(errors.New("手机号已被使用"))
		}
	}
	if email != "" {
		// 邮箱检查
		acc, err := dao.GetAccountsRepo().GetAccountByEmail(ctx, email)
		if err != nil {
			return nil, lueerror.PgErr(err)
		}
		if acc != nil && acc.GID != gID {
			return nil, lueerror.EmailHasUsed(errors.New("邮箱已经被使用"))
		}
	}
	// 校验验证码
	if code != "" && (!srv.cfg.Base.Debug || code != "6666") {
		if errL := srv.verifyCode(ctx, mobile, email, code, codeType, codeOrigin); errL != nil {
			return nil, errL
		}
	}

	acc, err := dao.GetAccountsRepo().GetAccountByGID(ctx, gID)
	if err != nil {
		return nil, lueerror.PgErr(err)
	}
	if acc == nil {
		return nil, lueerror.AccNotExit(errors.New("账号不存在"))
	}
	acc.UpdateInfo(mobile, email, userName, avatar, countryCode)
	err = dao.GetAccountsRepo().UpdateAccount(ctx, acc)
	if err != nil {
		return nil, lueerror.PgErr(err)
	}
	return srv.fieldsFormat(acc), nil
}

// CredibleSetMobileByGId 服务端可信设置手机号
func (srv *AccountsSrv) CredibleSetMobileByGId(ctx context.Context, gID, mobile string) lueerror.LueError {
	if mobile == "" {
		return lueerror.CodeVerifyFailed(errors.New("mobile 不为空"))
	}

	var acc *model.Accounts
	var err error
	acc, err = dao.GetAccountsRepo().GetAccountByGID(ctx, gID)
	if err != nil {
		return lueerror.PgErr(err)
	}
	if acc == nil {
		return lueerror.AccNotExit(errors.New("账号不存在"))
	}
	if acc.Status != 1 {
		return lueerror.AccNotExit(errors.New("账号状态不匹配"))
	}

	acc.SetMobile(mobile)
	err = dao.GetAccountsRepo().SetMobile(ctx, acc)
	if err != nil {
		return lueerror.PgErr(err)
	}
	return nil
}

// ResetPasswordByCode 根据验证码重置密码
func (srv *AccountsSrv) ResetPasswordByCode(ctx context.Context, mobile, email, password, code, codeType, codeOrigin string, isMD5 bool) lueerror.LueError {
	if code == "" {
		return lueerror.CodeVerifyFailed(errors.New("code 不为空"))
	}
	var acc *model.Accounts
	var err error
	if codeOrigin == "email" {
		acc, err = dao.GetAccountsRepo().GetAccountByEmail(ctx, email)
	} else {
		acc, err = dao.GetAccountsRepo().GetAccountByMobile(ctx, mobile)
	}
	if err != nil {
		return lueerror.PgErr(err)
	}
	if acc == nil {
		return lueerror.AccNotExit(errors.New("账号不存在"))
	}
	if code != "" && (!srv.cfg.Base.Debug || code != "6666") {
		if errL := srv.verifyCode(ctx, mobile, email, code, codeType, codeOrigin); errL != nil {
			return errL
		}
	}
	acc.SetPassword(password, isMD5)
	err = dao.GetAccountsRepo().ResetPassword(ctx, acc)
	if err != nil {
		return lueerror.PgErr(err)
	}
	return nil
}

// ResetPasswordByOld 根据旧密码修改
func (srv *AccountsSrv) ResetPasswordByOld(ctx context.Context, gID, password, oldPwd string, isMD5 bool) lueerror.LueError {
	if oldPwd == "" {
		return lueerror.InvalidPassword(errors.New("旧密码为空"))
	}
	acc, err := dao.GetAccountsRepo().GetAccountByGID(ctx, gID)
	if err != nil {
		return lueerror.PgErr(err)
	}
	if acc == nil {
		return lueerror.AccNotExit(err)
	}
	if !acc.PasswordVerify(oldPwd, isMD5) {
		return lueerror.InvalidPassword(errors.New("密码不正确"))
	}
	acc.SetPassword(password, isMD5)
	err = dao.GetAccountsRepo().ResetPassword(ctx, acc)
	if err != nil {
		return lueerror.PgErr(err)
	}
	return nil
}

// CheckMobile 检查手机号
func (srv *AccountsSrv) CheckMobile(ctx context.Context, mobile string) (map[string]interface{}, lueerror.LueError) {
	acc, err := dao.GetAccountsRepo().GetAccountByMobile(ctx, mobile)
	if err != nil {
		return nil, nil
	}
	ret := map[string]interface{}{
		"is_exist": false,
	}
	if acc == nil {
		return ret, nil
	}
	ret["g_id"] = acc.GID
	ret["mobile"] = acc.Mobile
	ret["user_name"] = acc.UserName
	ret["is_exist"] = true
	return ret, nil
}

// SendMobileCode 发送手机验证码
func (srv *AccountsSrv) SendMobileCode(ctx context.Context, mobile, codeType string) lueerror.LueError {
	err := dao.GetSmsRepo().SendMobileCode(ctx, mobile, codeType)
	if err != nil {
		return lueerror.MqErr(err)
	}
	return nil
}

// VerifyMobileCode 验证手机号
func (srv *AccountsSrv) VerifyMobileCode(ctx context.Context, mobile, code, codeType string) lueerror.LueError {
	isOk, err := dao.GetSmsRepo().VerifyMobileCode(ctx, mobile, code, codeType)
	if err != nil {
		return lueerror.GrpcErr(err)
	}
	if !isOk {
		return lueerror.CodeVerifyFailed(errors.New("验证码错误"))
	}
	return nil
}

func (srv *AccountsSrv) sendMpRegMessage(ctx context.Context, requestId, openId string) lueerror.LueError {
	title := fmt.Sprintf("欢迎注册%s", srv.cfg.Biz.Name)
	desc := "这是描述"
	url := fmt.Sprintf("%s?ticket=%s", srv.cfg.Biz.MpRegMsgUrl, requestId)
	err := dao.GetSmsRepo().SendMpNewMessage(ctx, srv.cfg.Biz.MpAppId, openId, title, desc, srv.cfg.Biz.MpRegMsgPicUrl, url)
	if err != nil {
		return lueerror.GrpcErr(err)
	}
	return nil
}

// SavePlatformUserInfo 暂存平台用户信息
func (srv *AccountsSrv) SavePlatformUserInfo(ctx context.Context, ticket, appId, openId, unionId, nickname, avatar, platform string) error {

	uInfo := &object.PlatformUserInfo{
		AppId:    appId,
		OpenId:   openId,
		UnionId:  unionId,
		Nickname: nickname,
		Avatar:   avatar,
		Platform: platform,
	}
	// 查询用户是否存在
	channel, err := dao.GetLoginChannelRepo().GetLoginChannelByOpenId(ctx, appId, openId, uInfo.GetInnerPlatform())
	if err != nil {
		return lueerror.PgErr(err)
	}
	if channel == nil {
		// 发送注册推送
		if errL := srv.sendMpRegMessage(ctx, ticket, openId); errL != nil {
			return errL
		}
	}
	err = dao.GetAccountsRepo().CachePlatformUser(ctx, ticket, uInfo)
	if err != nil {
		return lueerror.RedisErr(err)
	}
	return nil
}

// LoginWithTicket 根据 ticket 登陆
func (srv *AccountsSrv) LoginWithTicket(ctx context.Context, ticket string) (*object.LoginAccInfo, lueerror.LueError) {
	platUser, err := dao.GetAccountsRepo().GetPlatformUser(ctx, ticket)
	if err != nil {
		return nil, lueerror.RedisErr(err)
	}
	// 未扫码登陆
	ret := &object.LoginAccInfo{}
	if platUser == nil {
		ret.NeedScan()
		return ret, nil
	}
	loginCh, err := dao.GetLoginChannelRepo().GetLoginChannelByOpenId(ctx, platUser.AppId, platUser.OpenId, platUser.GetInnerPlatform())
	if err != nil {
		return nil, lueerror.PgErr(err)
	}
	// 需要注册
	if loginCh == nil {
		ret.NeedRegister()
		return ret, nil
	}
	acc, err := dao.GetAccountsRepo().GetAccountById(ctx, loginCh.AccId)
	if err != nil {
		return nil, lueerror.PgErr(err)
	}
	if acc == nil {
		return nil, lueerror.AccNotExit(errors.New("账号异常"))
	}
	// 构建返回值
	avatar := acc.Avatar
	if avatar == "" {
		avatar = loginCh.Avatar
	}
	loginInfo := object.NewLoginAccInfo(acc.GID, acc.Mobile, acc.Email, acc.UserName, loginCh.AppId, loginCh.OpenId, avatar, loginCh.Platform)
	loginInfo.OK()
	return loginInfo, nil
}

// 新增渠道
func (srv *AccountsSrv) addLoginChannel(ctx context.Context, accId int64, appId, openId, unionid, nickname, avatar string, platform int8) (*model.LoginChannel, lueerror.LueError) {
	// 查询渠道
	channel, err := dao.GetLoginChannelRepo().GetLoginChannelByOpenId(ctx, appId, openId, platform)
	if err != nil {
		return nil, lueerror.PgErr(err)
	}
	if channel != nil {
		if channel.AccId != accId {
			return nil, lueerror.InvalidChannel(errors.New("请直接登录"))
		}
		return channel, nil
	}
	// 新建渠道
	channel = model.NewLoginChannel(accId, platform, appId, openId, unionid, nickname, avatar)
	channel.Id, err = dao.GetLoginChannelRepo().CreateLoginChannel(ctx, channel)
	if err != nil {
		return nil, lueerror.PgErr(err)
	}
	return channel, nil
}

// RegisterWithTicket 根据 ticket 注册用户
func (srv *AccountsSrv) RegisterWithTicket(ctx context.Context, ticket, mobile, email, password, code, codeType,
	countryCode, codeOrigin string, isMD5 bool) (*object.LoginAccInfo, lueerror.LueError) {
	// 获取缓存数据
	platUser, err := dao.GetAccountsRepo().GetPlatformUser(ctx, ticket)
	if err != nil {
		return nil, lueerror.RedisErr(err)
	}

	if platUser == nil {
		return nil, lueerror.InvalidRequest(errors.New("注册用户缓存不存在"))
	}
	// 检查手机号邮箱
	acc, errL := srv.registerCheck(ctx, mobile, email)
	if errL != nil {
		return nil, errL
	}
	// 校验验证码
	if code != "" {
		if errL := srv.verifyCode(ctx, mobile, email, code, codeType, codeOrigin); errL != nil {
			return nil, errL
		}
	}

	ret, err := dao.Transaction(ctx, func(ctx context.Context) (interface{}, error) {
		// 用户存在，新增渠道
		var channel *model.LoginChannel
		if acc != nil {
			// 激活账号
			acc, errL = srv.activateAccount(ctx, acc)
			if errL != nil {
				return nil, errL
			}
			channel, err = srv.addLoginChannel(ctx, acc.ID, platUser.AppId, platUser.OpenId, platUser.UnionId, platUser.Nickname,
				platUser.Avatar, platUser.GetInnerPlatform())
			if err != nil {
				return nil, lueerror.PgErr(err)
			}
		} else {
			// 新建用户
			acc = model.NewAccounts(mobile, email, platUser.Nickname, platUser.Avatar, countryCode, platUser.Platform)
			_ = acc.SetStatus(model.AccountsOk)
			acc.SetPassword(password, isMD5)
			acc.ID, err = dao.GetAccountsRepo().CreateAccount(ctx, acc)
			if err != nil {
				return nil, lueerror.PgErr(err)
			}
			channel, err = srv.addLoginChannel(ctx, acc.ID, platUser.AppId, platUser.OpenId, platUser.UnionId, platUser.Nickname,
				platUser.Avatar, platUser.GetInnerPlatform())
			if err != nil {
				return nil, lueerror.PgErr(err)
			}
		}
		avatar := acc.Avatar
		if avatar == "" {
			avatar = channel.Avatar
		}
		// 构建返回值
		ret := object.NewLoginAccInfo(acc.GID, acc.Mobile, acc.Email, acc.UserName, channel.AppId, channel.OpenId,
			avatar, channel.Platform)
		ret.OK()
		return ret, nil
	})
	if err != nil {
		return nil, parseLueerror(err)
	}
	return ret.(*object.LoginAccInfo), nil
}

// GetLoginQrCode 获取登陆二维码
func (srv *AccountsSrv) GetLoginQrCode(ctx context.Context, appId string) (*object.WechatMpQrcode, lueerror.LueError) {
	ret, err := dao.GetLoginChannelRepo().GetMpLoginQrcode(ctx, appId)
	if err != nil {
		return nil, lueerror.GrpcErr(err)
	}
	return ret, nil
}

// AccountInvitation 邀请账号
func (srv *AccountsSrv) AccountInvitation(ctx context.Context, mobile, email, userName, countryCode, origin, avatar string) (*object.AccInfo, lueerror.LueError) {
	// 检查手机号
	acc, errL := srv.registerCheck(ctx, mobile, email)
	if errL != nil {
		return nil, errL
	}
	if acc != nil {
		return srv.fieldsFormat(acc), nil
	}
	var err error
	acc = model.NewAccounts(mobile, email, userName, avatar, countryCode, origin)
	_ = acc.SetStatus(model.AccountsUnActivate)
	acc.ID, err = dao.GetAccountsRepo().CreateAccount(ctx, acc)
	if err != nil {
		return nil, lueerror.PgErr(err)
	}
	return srv.fieldsFormat(acc), nil
}

// AccConfirmInvitation 确认邀请
func (srv *AccountsSrv) AccConfirmInvitation(ctx context.Context, gId, appId, openId, unionId, nickname, avatar, code, codeType, codeOrigin, platform string) (*object.AccInfo, lueerror.LueError) {
	// 获取账号
	acc, err := dao.GetAccountsRepo().GetAccountByGID(ctx, gId)
	if err != nil {
		return nil, lueerror.PgErr(err)
	}
	if acc == nil {
		return nil, lueerror.AccNotExit(err)
	}
	// 验证码校验
	if errL := srv.verifyCode(ctx, acc.Mobile, acc.Email, code, codeType, codeOrigin); errL != nil {
		return nil, errL
	}
	ret, err := dao.Transaction(ctx, func(ctx context.Context) (interface{}, error) {
		// 根据 open id 获取渠道
		// 新增或更新渠道
		plat, err := model.GetInnerPlatform(platform)
		channel, err := dao.GetLoginChannelRepo().GetLoginChannelByOpenId(ctx, appId, openId, plat)
		if channel == nil {
			// 将账号设置为激活状态
			if !acc.IsActivate() {
				acc.Activate()
				err = dao.GetAccountsRepo().UpdateAccount(ctx, acc)
				if err != nil {
					return nil, lueerror.PgErr(err)
				}
			}
			// 新增渠道
			_, errL := srv.addLoginChannel(ctx, acc.ID, appId, openId, unionId, nickname, avatar, plat)
			if errL != nil {
				return nil, errL
			}

		} else {
			// 用户曾经已经注册
			accO, err := dao.GetAccountsRepo().GetAccountById(ctx, channel.AccId)
			if err != nil {
				return nil, lueerror.PgErr(err)
			}
			// 将邀请账户的手机号挪过来
			if !acc.IsActivate() {
				accO.UpdateInfo(acc.Mobile, acc.Email, acc.UserName, acc.Avatar, acc.CountryCode)
				err = dao.GetAccountsRepo().UpdateAccount(ctx, accO)
				if err != nil {
					return nil, lueerror.PgErr(err)
				}
				// 删除未激活账号
				acc.Delete()
				err = dao.GetAccountsRepo().UpdateAccount(ctx, acc)
				if err != nil {
					return nil, lueerror.PgErr(err)
				}
			}
			acc = accO
		}
		return srv.fieldsFormat(acc), nil
	})
	if err != nil {
		return nil, parseLueerror(err)
	}
	return ret.(*object.AccInfo), nil
}
